package com.dwj.auto.comment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dwj.auto.comment.common.response.PageResponse;
import com.dwj.auto.comment.common.response.Response;
import com.dwj.auto.comment.common.utils.CodeUtil;
import com.dwj.auto.comment.config.mybatis.plus.CommonServiceImpl;
import com.dwj.auto.comment.dao.entity.Exam;
import com.dwj.auto.comment.dao.entity.StuClass;
import com.dwj.auto.comment.dao.entity.StuScore;
import com.dwj.auto.comment.dao.entity.Student;
import com.dwj.auto.comment.dao.mapper.ExamMapper;
import com.dwj.auto.comment.dao.mapper.StuClassMapper;
import com.dwj.auto.comment.dao.mapper.StuScoreMapper;
import com.dwj.auto.comment.dao.mapper.StudentMapper;
import com.dwj.auto.comment.model.form.ScoreForm;
import com.dwj.auto.comment.model.req.StuScoreCriteria;
import com.dwj.auto.comment.model.vo.StuScoreVo;
import com.dwj.auto.comment.service.IStuScoreService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dwjian
 * @since 2020-01-20
 */
@Service
public class StuScoreServiceImpl extends CommonServiceImpl<StuScoreMapper, StuScore> implements IStuScoreService {

    @Resource
    private StudentMapper studentMapper;
    @Resource
    private StuClassMapper stuClassMapper;
    @Resource
    private ExamMapper examMapper;

    @Override
    public PageResponse<StuScoreVo> getList(StuScoreCriteria criteria) {
        return PageResponse.success(this.voPage(criteria));
    }

    @Override
    public Response<String> edit(ScoreForm form) {
        Student student = studentMapper.selectById(form.getStuId());
        if(Objects.isNull(student)){
            return Response.fail("学生信息不存在");
        }
        StuClass stuClass = stuClassMapper.selectById(form.getClassId());
        if(Objects.isNull(stuClass)){
            return Response.fail("班级信息不存在");
        }
        Exam exam = examMapper.selectById(form.getExamId());
        if(Objects.isNull(exam)){
            return Response.fail("考试信息不存在");
        }
        StuScoreCriteria criteria = new StuScoreCriteria();
        criteria.setStuId(student.getId());
        criteria.setExamId(exam.getId());
        PageResponse<StuScoreVo> scoreResponse = getList(criteria);
        if(scoreResponse.getCount() > 0){
            return Response.fail("该学生成绩已存在");
        }
        if(StringUtils.isEmpty(form.getId())){
            return add(form, stuClass, student, exam);
        }
        StuScore score = new StuScore();
        BeanUtils.copyProperties(form, score);
        score.setStuCode(student.getStuCode());
        score.setStuName(student.getName());
        score.setClassName(stuClass.getClassName());
        score.setExamName(exam.getExamName());
        boolean update = this.updateById(score);
        if(update) {
            return Response.success();
        }
        return Response.fail("更新失败");
    }

    /**
     * 新增成绩
     * @param form
     * @param stuClass
     * @param exam
     * @return
     */
    private Response<String> add(ScoreForm form, StuClass stuClass, Student student, Exam exam){
        StuScore score = new StuScore();
        BeanUtils.copyProperties(form, score);
        score.setId(CodeUtil.uuid());
        score.setStuCode(student.getStuCode());
        score.setStuName(student.getName());
        score.setClassName(stuClass.getClassName());
        score.setExamName(exam.getExamName());
        boolean add = this.save(score);
        if(add) {
            return Response.success();
        }
        return Response.fail("新增失败");
    }

}
